package com.sanjin.filter;

import com.alibaba.fastjson2.JSON;
import com.sanjin.common.contants.HttpStatus;
import com.sanjin.common.result.Result;
import com.sanjin.support.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


// 登录用户权限不足报错
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        Result<Object> result = Result.error("权限不足", HttpStatus.FORBIDDEN);
        String resultJson = JSON.toJSONString(result);
        WebUtils.renderString(httpServletResponse,resultJson);
    }
}
